package com.zack.gank.gankandroid.main.find;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.zack.gank.gankandroid.MyApplication;
import com.zack.gank.gankandroid.gankandroid.R;
import com.zack.gank.gankandroid.main.LzyFragment;
import com.zack.gank.gankandroid.main.find.adapter.SelectFindAdapter;
import com.zack.gank.gankandroid.main.find.bean.Gank;
import com.zack.gank.gankandroid.main.find.bean.ImageGank;
import com.zack.gank.gankandroid.main.find.bean.TodayGank;
import com.zack.gank.gankandroid.utils.TimeUtils;

import java.util.ArrayList;

/**
 * Created by Android on 2017/7/13.
 * <p/>
 * 精选
 */
public class SelectFindFragment extends LzyFragment implements FindSelectView {

    RecyclerView rlvFind;
    SmartRefreshLayout srlSelectFind;
    private FindSelectPresenter findPresenter;
    SelectFindAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_find_select, container, false);
        rlvFind= (RecyclerView) view.findViewById(R.id.rlv_find);
        srlSelectFind= (SmartRefreshLayout) view.findViewById(R.id.srl_select_find);
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    protected void loadData() {
        Logger.v("SelectFindFragment");
       if(findPresenter==null){
            findPresenter=new FindSelectPresenter(this);
        }
        findPresenter.setGankDate(TimeUtils.getNowString());


    }

    @Override
    public void onSucess(TodayGank todayGank) {
        // 成功
//        Logger.v(todayGank.results.妹纸List.get(0).url);
        Logger.v("onSucess");
        final ArrayList<Gank> list =new ArrayList<>();

        if(todayGank!=null){
            if(todayGank.results.androidList!=null){
                for (int i=0;i<todayGank.results.androidList.size();i++){
                    list.add(todayGank.results.androidList.get(i));
                }
            }
            if(todayGank.results.iOSList!=null){
                for (int i=0;i<todayGank.results.iOSList.size();i++){
                    list.add(todayGank.results.iOSList.get(i));
                }
            }
            if(todayGank.results.妹纸List!=null){
                for (int i=0;i<todayGank.results.妹纸List.size();i++){
                    list.add(todayGank.results.妹纸List.get(i));
                }
            }
            if(todayGank.results.休息视频List!=null){
                for (int i=0;i<todayGank.results.休息视频List.size();i++){
                    list.add(todayGank.results.休息视频List.get(i));
                }
            }
            Logger.v("lskkdfk");
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    /**
                     *   TODO
                     * 下代码 出现问题 ，如fragment重新创建 adapter.notifyDataSetChanged(); 不能达到效果
                     * 因为 RecyclerView 被 重新创建了
                     * 解决方案
                     * http://www.jianshu.com/p/8a0f6b627e37?winzoom=1
                     */
                   /* if(adapter==null){
                        adapter = new SelectFindAdapter(getContext(),list);
                        rlvFind.setAdapter(adapter);
                        rlvFind.setLayoutManager(new LinearLayoutManager(getContext()));
                    }else{
                        adapter.notifyDataSetChanged();
                    }*/
                    adapter = new SelectFindAdapter(getContext(),list);
                    rlvFind.setAdapter(adapter);
                    rlvFind.setLayoutManager(new LinearLayoutManager(getContext()));
                }
            });




        }

    }

    @Override
    public void onImageSucess(ImageGank imageGank) {
        // 获取图片成功
//        MyApplication.getContext().setComonImageUrl(imageGank.results);
    }

    @Override
    public void onFile(Throwable throwable) {
        // 失败
    }
}
